package com.example.demo.controller;

import com.example.demo.Exception.ProductAdminException;
import com.example.demo.dao.SupplierJPARepository;
import com.example.demo.entity.Supplier;
import com.example.demo.service.SupplierServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@Api(tags = "供应商管理")
@RestController
@RequestMapping("suppliers")
public class SupplierController {
    @Autowired
    SupplierServer supplierServer;

    @ApiOperation("根据Id查询供应商")
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@ApiParam("供应商Id") @PathVariable long id){
        Supplier result = supplierServer.getSupplier(id);
        if(result==null){
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("根据条件查询供应商")
    @GetMapping("")
    public Page<Supplier> findSupplier(@ApiParam("商品名称")String name,
                                       @ApiParam("页码")@RequestParam(defaultValue = "0") Integer pageNum,
                                       @ApiParam("每页记录数")@RequestParam(defaultValue = "10") Integer pageSize){
        return supplierServer.findSuppliers(name, PageRequest.of(pageNum,pageSize, Sort.by("id")));
    }

    @ApiOperation("添加供应商")
    @PostMapping("")
    public Supplier addSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        return supplierServer.addSuplier(supplier);
    }

    @ApiOperation("修改供应商")
    @PutMapping("/{id}")
    public void updateSupplier(@PathVariable long id,@RequestBody Supplier supplier) throws ProductAdminException {
        supplierServer.updateSuolier(id,supplier);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public void deleteSupplier(@PathVariable long id) throws ProductAdminException {
        supplierServer.deleteSupplier(id);
    }


}
